/*
 * CreateUserDialog.java
 *
 * Created on 22 oktober 2009, 23:45
 */

package mop.gui.task;

import mop.exception.IllegalStatusException;
import javax.swing.JOptionPane;
import mop.controller.TaskController;
import mop.gui.main.MainWindow;

/**
 *
 * @author  Laurens
 */
public class TaskStatusDialog extends javax.swing.JDialog {
    
    private String task;

    /** Creates new form CreateUserDialog */
    public TaskStatusDialog(java.awt.Frame parent, boolean modal, String task) {
        super(parent, modal);
        initComponents();
        this.task = task;
        this.txtTask.setText(task);
        this.cmbStatus.setSelectedItem( TaskController.getInstance().getStatus(task) );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTaskStatus = new javax.swing.JLabel();
        lblTask = new javax.swing.JLabel();
        btnUpdate = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtTask = new javax.swing.JTextArea();
        lblNewStatus = new javax.swing.JLabel();
        cmbStatus = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Update Task Status");

        lblTaskStatus.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblTaskStatus.setText("Update Task Status");
        lblTaskStatus.setName("lblTaskStatus"); // NOI18N

        lblTask.setText("Task");
        lblTask.setName("lblTask"); // NOI18N

        btnUpdate.setText("Update");
        btnUpdate.setName("btnUpdate"); // NOI18N
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtTask.setColumns(20);
        txtTask.setEditable(false);
        txtTask.setRows(5);
        txtTask.setName("txtTask"); // NOI18N
        jScrollPane1.setViewportView(txtTask);

        lblNewStatus.setText("Choose new status");
        lblNewStatus.setName("lblNewStatus"); // NOI18N

        cmbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Unfinished", "Successful", "Failed" }));
        cmbStatus.setName("cmbStatus"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                    .addComponent(lblTaskStatus)
                    .addComponent(lblTask)
                    .addComponent(lblNewStatus)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cmbStatus, 0, 327, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTaskStatus)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblTask)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblNewStatus)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUpdate)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

/**
 * Closes this dialog.
 * 
 * @param   evt
 */
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
    try {
        TaskController.getInstance().updateStatus(task, (String) this.cmbStatus.getSelectedItem());
        MainWindow.getInstance().update();
        JOptionPane.showMessageDialog(null, "Status updated!", "Success", JOptionPane.INFORMATION_MESSAGE);
        this.dispose();
    } catch (IllegalStatusException ex) {
        JOptionPane.showMessageDialog(null, "Bad status!", "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_btnUpdateActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox cmbStatus;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNewStatus;
    private javax.swing.JLabel lblTask;
    private javax.swing.JLabel lblTaskStatus;
    private javax.swing.JTextArea txtTask;
    // End of variables declaration//GEN-END:variables

}
